﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import Classes.*;
    import Classes.inventory.*;
    import Modules.stats.types.*;

    public class StorageSupplyMechanic implements IActionGameMechanic, IMultiPickSupporter, IToolTipModifier {

        protected var m_owner:MechanicMapResource;
        protected var m_config:MechanicConfigData;

        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return (((!(this.hasHarvestState())) && (this.hasStores())));
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local12:Item;
            var _local13:String;
            var _local14:MapResource;
            var _local3:Boolean;
            var _local4:Object = {operation:"supply"};
            var _local5:Boolean;
            if ("true" == this.m_config.params["blockOthers"]){
                _local5 = true;
            };
            var _local6:Array = this.m_owner.getDataForMechanic(this.m_config.params["storageSource"]);
            var _local7:Object = this.m_owner.getDataForMechanic(this.m_config.params["harvestStateName"]);
            var _local8:int;
            var _local9:Array = new Array();
            if (_local6){
                _local12 = null;
                _local13 = null;
                for each (_local14 in _local6) {
                    _local13 = _local14.getItemName();
                    _local12 = Global.gameSettings.getItemByName(_local13);
                    if (_local12){
                        _local9.push(_local13);
                        _local8 = (_local8 + _local12.commodityReq);
                        if (_local14.canCountUpgradeActions()){
                            _local14.incrementUpgradeActionCount(false);
                        };
                    };
                };
            };
            var _local10:int = Global.player.commodities.getCount("goods");
            if (_local10 >= _local8){
                _local3 = true;
            };
            var _local11:String;
            if (_local3){
                _local7 = {
                    customers:0,
                    customersReq:_local8,
                    resources:_local9
                };
                _local11 = ZLoc.t("Main", "StartingDelivery", {amount:_local8});
                this.m_owner.displayStatus(_local11);
                Global.player.commodities.remove("goods", _local8);
                this.m_owner.setDataForMechanic(this.m_config.params["harvestStateName"], _local7);
                this.m_owner.trackAction(TrackedActionType.HARVEST);
                if ((this.m_owner is IMerchant)){
                    (this.m_owner as IMerchant).updatePeepSpawning();
                };
                Global.ui.dispatchEvent(new GenericObjectEvent(GenericObjectEvent.STORED_ITEM_SUPPLY, null));
            } else {
                _local11 = ZLoc.t("Main", "NeedCommodityToOpenAmount", {amount:_local8});
                this.m_owner.setDataForMechanic(this.m_config.params["supplyStateName"], _local7);
                this.m_owner.displayStatus(_local11, null, 0xFF0000);
            };
            return (new MechanicActionResult(_local3, !(_local5), _local3, _local4));
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        public function getPick():String{
            return (this.m_config.params["pick"]);
        }
        public function getToolTipStatus():String{
            var _local2:int;
            var _local1:String;
            if (((!(this.hasHarvestState())) && (this.hasStores()))){
                _local2 = this.getRequiredCommodity();
                _local1 = ZLoc.t("Main", "NeedCommodityToOpenAmount", {amount:_local2});
            };
            return (_local1);
        }
        public function getToolTipAction():String{
            var _local2:int;
            var _local3:int;
            var _local1:String;
            if (((!(this.hasHarvestState())) && (this.hasStores()))){
                _local2 = this.getRequiredCommodity();
                _local3 = Global.player.commodities.getCount("goods");
                if (_local3 >= _local2){
                    _local1 = ZLoc.t("Main", "ClickToSupply");
                } else {
                    _local1 = Commodities.getNoCommoditiesTooltip();
                };
            };
            return (_local1);
        }
        protected function hasHarvestState():Boolean{
            var _local1:Boolean;
            var _local2:Object = this.m_owner.getDataForMechanic(this.m_config.params["harvestStateName"]);
            if (_local2){
                _local1 = true;
            };
            return (_local1);
        }
        protected function hasStores():Boolean{
            var _local1:Boolean;
            var _local2:Array = this.m_owner.getDataForMechanic(this.m_config.params["storageSource"]);
            if (((_local2) && ((_local2.length > 0)))){
                _local1 = true;
            };
            return (_local1);
        }
        protected function getRequiredCommodity():int{
            var _local5:MapResource;
            var _local1:int;
            var _local2:Item;
            var _local3:String;
            var _local4:Array = this.m_owner.getDataForMechanic(this.m_config.params["storageSource"]);
            for each (_local5 in _local4) {
                _local3 = _local5.getItemName();
                _local2 = Global.gameSettings.getItemByName(_local3);
                if (_local2){
                    _local1 = (_local1 + _local2.commodityReq);
                };
            };
            return (_local1);
        }

    }
}//package Mechanics.GameEventMechanics 
